package com.rdzn.mall.portal.service.impl;

import com.rdzn.mall.base.util.RedisKeyUtil;
import com.rdzn.mall.base.util.RedisUtil;
import com.rdzn.mall.center.entity.*;
import com.rdzn.mall.center.service.*;
import com.rdzn.mall.portal.domain.HomeContentResult;
import com.rdzn.mall.portal.domain.HomeFlashPromotion;
import com.rdzn.mall.portal.service.IHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 首页内容管理Service实现类
 * Created by dingzg on 2019/1/28.
 */
@Service
public class HomeServiceImpl implements IHomeService {
//    @Autowired
//    private SmsHomeAdvertiseMapper advertiseMapper;
//    @Autowired
//    private HomeDao homeDao;
//    @Autowired
//    private SmsFlashPromotionMapper flashPromotionMapper;
//    @Autowired
//    private SmsFlashPromotionSessionMapper promotionSessionMapper;
//    @Autowired
//    private PmsProductMapper productMapper;
//    @Autowired
//    private PmsProductCategoryMapper productCategoryMapper;
//    @Autowired
//    private CmsSubjectMapper subjectMapper;

    @Autowired
    private ISmsHomeAdvertiseService smsHomeAdvertiseService;
    @Autowired
    private IPmsProductCategoryService productCategoryService;

    @Autowired
    private ISmsHomeBrandService homeBrandService;

    @Autowired
    private ISmsHomeNewProductService homeNewProductService;
    @Autowired
    private ISmsHomeRecommendProductService homeRecommendProductService;
    @Autowired
    private ISmsHomeRecommendSubjectService homeRecommendSubjectService;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public HomeContentResult content() {
        String key = redisUtil.getKey(RedisKeyUtil.HOME_INDEX);
        if (redisUtil.hasKey(key)){
            return (HomeContentResult)redisUtil.get(key);
        }
        HomeContentResult result = new HomeContentResult();
        //获取首页广告
        result.setAdvertiseList(smsHomeAdvertiseService.getList());
        //商品分类
        result.setCategories(productCategoryService.listHome());
        //获取推荐品牌
        result.setBrandList(homeBrandService.getList());
//        //获取秒杀信息
//        result.setHomeFlashPromotion(getHomeFlashPromotion());
        //获取新品推荐
        result.setNewProductList(homeNewProductService.getList());
        //获取人气推荐
        result.setHotProductList(homeRecommendProductService.getList());
        //获取推荐专题
        result.setSubjectList(homeRecommendSubjectService.getList());
        redisUtil.set(key, result);
        return result;
    }

    @Override
    public List<PmsProduct> recommendProductList(Integer pageSize, Integer pageNum) {
        // TODO: 2019/1/29 暂时默认推荐所有商品
//        PageHelper.startPage(pageNum,pageSize);
//        PmsProductExample example = new PmsProductExample();
//        example.createCriteria()
//                .andDeleteStatusEqualTo(0)
//                .andPublishStatusEqualTo(1);
//        return productMapper.selectByExample(example);
        return null;
    }

    @Override
    public List<PmsProductCategory> getProductCateList(Long parentId) {
//        PmsProductCategoryExample example = new PmsProductCategoryExample();
//        example.createCriteria()
//                .andShowStatusEqualTo(1)
//                .andParentIdEqualTo(parentId);
//        example.setOrderByClause("sort desc");
//        return productCategoryMapper.selectByExample(example);
        return null;
    }

    @Override
    public List<CmsSubject> getSubjectList(Long cateId, Integer pageSize, Integer pageNum) {
//        PageHelper.startPage(pageNum,pageSize);
//        CmsSubjectExample example = new CmsSubjectExample();
//        CmsSubjectExample.Criteria criteria = example.createCriteria();
//        criteria.andShowStatusEqualTo(1);
//        if(cateId!=null){
//            criteria.andCategoryIdEqualTo(cateId);
//        }
//        return subjectMapper.selectByExample(example);
        return null;
    }

    @Override
    public List<PmsProduct> hotProductList(Integer pageNum, Integer pageSize) {
//        int offset = pageSize * (pageNum - 1);
//        return homeDao.getHotProductList(offset, pageSize);
        return null;
    }

    @Override
    public List<PmsProduct> newProductList(Integer pageNum, Integer pageSize) {
//        int offset = pageSize * (pageNum - 1);
//        return homeDao.getNewProductList(offset, pageSize);
        return null;
    }

    private HomeFlashPromotion getHomeFlashPromotion() {
        HomeFlashPromotion homeFlashPromotion = new HomeFlashPromotion();
        //获取当前秒杀活动
//        Date now = new Date();
//        SmsFlashPromotion flashPromotion = getFlashPromotion(now);
//        if (flashPromotion != null) {
//            //获取当前秒杀场次
//            SmsFlashPromotionSession flashPromotionSession = getFlashPromotionSession(now);
//            if (flashPromotionSession != null) {
//                homeFlashPromotion.setStartTime(flashPromotionSession.getStartTime());
//                homeFlashPromotion.setEndTime(flashPromotionSession.getEndTime());
//                //获取下一个秒杀场次
//                SmsFlashPromotionSession nextSession = getNextFlashPromotionSession(homeFlashPromotion.getStartTime());
//                if(nextSession!=null){
//                    homeFlashPromotion.setNextStartTime(nextSession.getStartTime());
//                    homeFlashPromotion.setNextEndTime(nextSession.getEndTime());
//                }
//                //获取秒杀商品
//                List<FlashPromotionProduct> flashProductList = homeDao.getFlashProductList(flashPromotion.getId(), flashPromotionSession.getId());
//                homeFlashPromotion.setProductList(flashProductList);
//            }
//        }
        return homeFlashPromotion;
    }

    //获取下一个场次信息
    private SmsFlashPromotionSession getNextFlashPromotionSession(Date date) {
//        SmsFlashPromotionSessionExample sessionExample = new SmsFlashPromotionSessionExample();
//        sessionExample.createCriteria()
//                .andStartTimeGreaterThan(date);
//        sessionExample.setOrderByClause("start_time asc");
//        List<SmsFlashPromotionSession> promotionSessionList = promotionSessionMapper.selectByExample(sessionExample);
//        if (!CollectionUtils.isEmpty(promotionSessionList)) {
//            return promotionSessionList.get(0);
//        }
        return null;
    }


    //根据时间获取秒杀活动
    private SmsFlashPromotion getFlashPromotion(Date date) {
//        Date currDate = DateUtil.getDate(date);
//        SmsFlashPromotionExample example = new SmsFlashPromotionExample();
//        example.createCriteria()
//                .andStatusEqualTo(1)
//                .andStartDateLessThanOrEqualTo(currDate)
//                .andEndDateGreaterThanOrEqualTo(currDate);
//        List<SmsFlashPromotion> flashPromotionList = flashPromotionMapper.selectByExample(example);
//        if (!CollectionUtils.isEmpty(flashPromotionList)) {
//            return flashPromotionList.get(0);
//        }
        return null;
    }

    //根据时间获取秒杀场次
    private SmsFlashPromotionSession getFlashPromotionSession(Date date) {
//        Date currTime = DateUtil.getTime(date);
//        SmsFlashPromotionSessionExample sessionExample = new SmsFlashPromotionSessionExample();
//        sessionExample.createCriteria()
//                .andStartTimeLessThanOrEqualTo(currTime)
//                .andEndTimeGreaterThanOrEqualTo(currTime);
//        List<SmsFlashPromotionSession> promotionSessionList = promotionSessionMapper.selectByExample(sessionExample);
//        if (!CollectionUtils.isEmpty(promotionSessionList)) {
//            return promotionSessionList.get(0);
//        }
        return null;
    }
}
